# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui

#_______________________________________________________________________________
class Button(QtGui.QPushButton):
    def __init__(self, parent=None):
        QtGui.QPushButton.__init__(self, parent)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, 
                                       QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
        self.setSizePolicy(sizePolicy)
        self.ctrl = None
        self.setEnabled(False)
    
    def add(self, layout):
        layout.addWidget(self)
    
#    def set_controller(self, ctrl):
#        self.ctrl = ctrl
#        self.setEnabled(True)
    
    def set_icon(self, url_resource):
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(url_resource), 
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.setIcon(icon)
    
    def set_name(self, name):
        self.setObjectName(name)
        
#_______________________________________________________________________________
class VolumeButton(Button):
    volume_muted = QtCore.pyqtSignal()
    
    def __init__(self, parent):
        Button.__init__(self, parent)
        self.muted = False
    
    def toggle_icon(self):
        if self.muted is True:
            self.set_icon(":/images/speaker_on.png")
            self.muted = False 
        else:
            self.set_icon(":/images/speaker_off.png") 
            self.muted = True
    
    def mouseReleaseEvent(self, me):
        rect = self.rect()
        if rect.contains(me.pos()) is True:
#            self.ctrl.mute()
            self.volume_muted.emit()
            self.toggle_icon()
            self.setDown(False)

#_______________________________________________________________________________
class PlayButton(Button):
    button_clicked = QtCore.pyqtSignal()
    
    def __init__(self, parent=None):
        Button.__init__(self, parent)
        
    def toggle_icon(self, state):
        if state != "play":
            self.set_icon(":/images/play.png"), 
        else:
            self.set_icon(":/images/pause.png"), 
    
    def mouseReleaseEvent(self, me):
#        if self.ctrl is None:
#            return
        rect = self.rect()
        if rect.contains(me.pos()) is True:
            self.button_clicked.emit()
#            self.ctrl.play_pause()
            self.setDown(False)
    
#_______________________________________________________________________________
class ProgressBar(QtGui.QProgressBar):
    pos_seeked = QtCore.pyqtSignal(int)
    
    def __init__(self, parent=None):
        QtGui.QProgressBar.__init__(self, parent)
        self.ctrl = None
        self.setEnabled(False)
    
#    def set_controller(self, ctrl):
#        self.ctrl = ctrl
#        self.setEnabled(True)
    
    def mousePressEvent(self, me):
#        if self.ctrl is None:
#            return
        width_px = float(self.width())
        x_px = float(me.x())
        width = float(self.maximum())
        x = int((x_px * width) / width_px)
        self.pos_seeked.emit(x)
#        self.ctrl.seek(x)
        
#_______________________________________________________________________________
class VolumeBar(QtGui.QProgressBar):
    volume_changed = QtCore.pyqtSignal(int)
    
    def __init__(self, parent=None):
        QtGui.QProgressBar.__init__(self, parent)
        self.width_ = float(100)
        self.ctrl = None
        self.setEnabled(False)
        
#    def set_controller(self, ctrl):
#        self.ctrl = ctrl
#        self.setEnabled(True)
    
    def mousePressEvent(self, me):
        width_px = float(self.width())
        x_px = float(me.x())
        volume = int((x_px * self.width_) / width_px)
#        self.ctrl.set_volume(volume)
        self.volume_changed.emit(volume)
        
    def mouseMoveEvent(self, me):
        width_px = float(self.width())
        x_px = float(me.x())
        volume = int((x_px * self.width_) / width_px)
        if 0 <= volume <= 100:
            self.volume_changed.emit(volume)
#            self.ctrl.set_volume(volume)
    
#_______________________________________________________________________________
class PlayerView(QtCore.QObject):
    def __init__(self, ui):
        QtCore.QObject.__init__(self)
        self.prev_button = ui.prev_button
        self.prev_button.setEnabled(False)
        self.play_button = ui.play_button
        self.next_button = ui.next_button
        self.next_button.setEnabled(False)
        self.stop_button = ui.stop_button
        self.stop_button.setEnabled(False)
        self.volume_button = ui.volume_button
        self.volume_button.setEnabled(False)
        self.volume_bar = ui.volume_bar
        self.progress_bar = ui.progress_bar
